package com.aps.server.utils.converter;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.aps.common.model.standardAps.StandardApsOrder;
import com.aps.common.model.standardAps.StandardApsOrderProcess;
import com.aps.server.entity.ApsOrder;
import com.aps.server.entity.BasicOperation;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author jsy
 * @description: 订单转换器
 * @date 2025/9/22 14:19
 */
public class OrderConverter {
    /**
     * @description: 将List<ApsOrder>转换为List<StandardApsOrder>
     * @author: jsy
     * @time: 2025/9/22 14:11
     */
    public static List<StandardApsOrder> convert(List<ApsOrder> apsOrders, List<BasicOperation> basicOperations, LocalDateTime globalStartTime) {
        // 处理null情况，返回空列表而不是null更安全
        if (CollUtil.isEmpty(apsOrders)) {
            return List.of();
        }
        // 将basicOperations按productCode分组，便于快速查询
        Map<String, List<BasicOperation>> productToOperations = CollUtil.isEmpty(basicOperations)
                ? new HashMap<>()
                : basicOperations.stream()
                .filter(operation -> StringUtils.isNotBlank(operation.getProductCode()))
                .collect(Collectors.groupingBy(BasicOperation::getProductCode));

        // 使用Stream进行转换，同时传递对应的工序列表
        return apsOrders.stream()
                .filter(ObjectUtil::isNotEmpty)  // 过滤null元素
                .map(apsOrder -> {
                    // 获取当前ApsOrder的productCode
                    String productCode = apsOrder.getProductCode();
                    // 根据productCode查找对应的工序列表，无匹配则返回空列表
                    List<BasicOperation> matchedOperations = productToOperations.getOrDefault(productCode, List.of());
                    // 传递ApsOrder和匹配的工序列表到转换方法
                    return OrderConverter.convertSingle(apsOrder, matchedOperations, globalStartTime);
                })
                .collect(Collectors.toList());
    }

    /**
     * @description: 将单个ApsOrder转换为StandardApsOrder
     * @author: jsy
     * @time: 2025/9/22 14:17
     */
    private static StandardApsOrder convertSingle(ApsOrder apsOrder, List<BasicOperation> basicOperations, LocalDateTime globalStartTime) {
        StandardApsOrder order = new StandardApsOrder();

        // 手动映射所有字段，这里假设了一些常见字段，实际应根据真实类结构修改
        order.setId(String.valueOf(apsOrder.getId()));
        order.setQuantity(Integer.parseInt(apsOrder.getQuantity()));
        order.setDueDate(apsOrder.getDeliveryDate().atStartOfDay());
        order.setStartDate(globalStartTime);
        // 处理嵌套列表
        order.setOrderProcessList(convertProcesses(basicOperations, order.getId()));

        return order;
    }

    /**
     * @description: 转换工序列表
     * @author: jsy
     * @time: 2025/9/22 14:22
     */
    private static List<StandardApsOrderProcess> convertProcesses(List<BasicOperation> basicOperations, String orderId) {
        if (Objects.isNull(basicOperations)) {
            return List.of();
        }

        return basicOperations.stream()
                .filter(Objects::nonNull)
                .map(process -> {
                    StandardApsOrderProcess orderProcess = new StandardApsOrderProcess();
                    // 手动映射工序字段
                    orderProcess.setId(String.valueOf(process.getId()));
                    orderProcess.setOrderId(orderId);
                    orderProcess.setEquipmentType(process.getEquipmentTypeRequire());
                    orderProcess.setProcessName(process.getOperationName());
                    orderProcess.setSequence(process.getSequence());
                    orderProcess.setUnitProcessingTime(process.getStandardTime().doubleValue());
                    return orderProcess;
                })
                .collect(Collectors.toList());
    }
}
